import { Injectable } from '@nestjs/common';
import { InRecord, Prisma } from '@prisma/client';
import { PrismaService } from 'src/db/prisma.service';
import { requestPromise } from 'src/interface';

@Injectable()
export class StatisticsService {
  constructor(private prisma: PrismaService) {}

  async findAll(params: {
    pageNum?: number;
    pageSize?: number;
    cursor?: Prisma.InRecordWhereUniqueInput;
    outBoundNo?: string;
    type?: number;
    createAt?: Date;
    orderBy?: Prisma.InRecordOrderByWithRelationInput;
  }): Promise<requestPromise> {
    const {
      pageNum = 1,
      pageSize = 10,
      cursor,
      orderBy,
      outBoundNo,
      type,
      createAt,
    } = params;
    const createAtTime = {
      gte: new Date(createAt[0]),
      lte: new Date(createAt[1]),
    };
    console.log(params, 'params', createAt);
    let where = {
      outBoundNo,
      type,
    };
    if (createAt[0] && createAt[1]) {
      where['createAt'] = createAtTime;
    }
    const totalInRecordCount = await this.prisma.inRecord.count({
      where,
    });
    const InRecords = await this.prisma.inRecord.findMany({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
      cursor,
      where,
      orderBy,
      select: {
        id: true,
        remark: true,
        outBoundNo: true,
        supplierId: true,
        type: true,
        createAt: true,
        InGoods: {
          select: {
            id: true,
            goodName: true,
            goodId: true,
            Sku:true,
          },
        },
      },
    });
    return {
      code: 200,
      message: 'success',
      data: {
        list: InRecords,
        total: totalInRecordCount,
      },
    };
  }
  async StaticIndex(timeArr) {
    const goodsCount = await this.prisma.good.count();
    const totalGoods = await this.prisma.sku.findMany({});
    let totalGoodsNum = totalGoods.reduce((pre, cur) => pre + Number(cur.quantity), 0);
    const recordList = await this.prisma.inRecord.findMany({
      where: {
        createAt: {
          gte: new Date(timeArr[0]),
          lte: new Date(timeArr[1] + 'T23:59:59.999Z'),
        },
      },
      select: {
        id: true,
        outBoundNo: true,
        type: true,
        createAt: true,
        supplierId: true,
        InGoods: {
          select: {
            id: true,
            goodName: true,
            goodId: true,
            Sku:true,
          },
        },
      },
    });
    //10进货 //20出货 // 30日常销售 //40退货 //50 损耗
    let typeArr = [20, 30, 50]; //出货
    let outObj = recordList
      .filter((item) => typeArr.includes(item.type))
      .map((ite) => {
        let Count = ite.InGoods.length;
        console.log(ite.InGoods, 'ite.InGoods');
        let total = 0;
        ite.InGoods.map(item => {
          console.log(item.Sku, 'item.Sku');
          if(Array.isArray(item.Sku)){
            item.Sku.map(async (sku: { id: string; quantity: string }) => {
              total += Number(sku.quantity);
            });
          }
        });
        return {
          total,
          Count,
        };
      });
    let InObj = recordList
      .filter((item) => !typeArr.includes(item.type))
      .map((ite) => {
        let Count = ite.InGoods.length;
        let total = 0;
        ite.InGoods.map(item => {
          console.log(item.Sku, 'item.Sku');
          if(Array.isArray(item.Sku)){
            item.Sku.map(async (sku: { id: string; quantity: string }) => {
              total += Number(sku.quantity);
            });
          }
        });
        return {
          total,
          Count,
        };
      });
    // console.log(totalGoods, 'totalGoods', recordList);
    return {
      code: 200,
      message: 'success',
      data: {
        goodsCount: goodsCount,
        totalGoodsNum: totalGoodsNum,
        outList: {
          times: outObj.length,
          total: outObj.reduce((pre, cur) => {
            return pre + cur.total;
          }, 0),
          Count: outObj.reduce((pre, cur) => {
            return pre + cur.Count;
          }, 0),
        },
        InObj: {
          times: InObj.length,
          total: InObj.reduce((pre, cur) => {
            return pre + cur.total;
          }, 0),
          Count: InObj.reduce((pre, cur) => {
            return pre + cur.Count;
          }, 0),
        },
      },
    };
  }
}
